/**
 * 传入的数据对象参数示例
 * {appendDoms: ['hdBanner', 'ftBanner'], hdBanner: { logo: url, title: string }, ftBanner: { img: url, title: string }, icons:{close: url} }
 * 
 * 参数解释：
 *  appendDoms：需要添加的区块。 
 *      参数类型：数组。 
*       可选值：'hdBannder', 'ftBanner', 'offlineCacheBtn', 'commentsBtn', 'chapterEndTip', 'commicEndTip'
 *  options：对dom结构的内容配置
 *      hdBanner：{logo: url, title: string}, 配置顶部banner的logo，文案。文案无长度限制，溢出显示点点点。
 *      ftBanner：{logo: url, title: string}, 配置底部banner的logo，文案。文案无长度限制，溢出显示点点点。
 * 
 * 在需要点击弹出浮层的元素上必须加上".km-pop"类名以及弹出浮层的类型.
 *  可选类型：cache, share, read1st, read 。 例：data-poptype="share"
 * 
 */
; (function ($, window, document, options) {
    var defaults = {
        doms: {
            hdBanner: function (options) { return '<div class="km-hd-banner km-banner"><img class="km-banner-logo" src="/static/bannerImg/hd_logo.png"><span class="km-banner-desc">' + options.title + '</span><div class="km-banner-operate"><a class="km-operate-open km-app">打开</a><i class="km-operate-close km-close" data-name="hdBanner"></i></div></div>' },
            rdHdBanner: function (options) { return '<div class="km-rdhd-banner km-banner"><img class="km-banner-logo" src="/static/bannerImg/hd_logo.png"><span class="km-banner-desc">' + options.title + '</span><div class="km-banner-operate"><a class="km-operate-open km-app">打开</a><i class="km-operate-close km-close" data-name="rdHdBanner"></i></div></div>' },
            ftBanner: function (options) { return '<div class="km-ft-banner km-banner ftBannerAnimHidden"><img class="km-banner-logo" src="' + options.img + '" alt=""><div class="km-banner-container"><span class="km-banner-desc">' + options.title + '</span><div class="km-banner-operate"><a class="km-operate-open km-app">打开</a><i class="km-operate-close km-close" data-name="ftBanner"></i></div></div></div>' },
            offlineCacheBtn: function (options) { return '<button class="km-offlinecache km-pop" data-poptype="cache">' + options.title + '<i class="km-star"></i></button>' },
            commentsBtn: function (options) { return '<div class="km-commont-btn"><button class="km-btn km-app">' + options.wordTips + '</button></div>' },
            chapterEndTip: function (options) { return '<img style="width: 100%;vertical-align: top;" src="/static/bannerImg/chapter_end_img_'+[1, 2, 3, 4][Math.floor(Math.random() * 4)]+'.png"><button class="km-btn-open km-app">打开</button>' },
            commicEndTip: function (options) { return '<div class="km-commic-end"><div class="km-container"><img class="logo" src="/static/bannerImg/end_image.png" alt=""><div class="km-buttons"><button class="km-btn-open km-app">打开APP订阅更新</button><button class="km-btn-close km-close" data-poptype="commicEndPage">继续用网页看漫画</button></div><img class="km-bottom-logo" src="/static/bannerImg/logo_bottom_km.png" alt=""></div></div>' },
            popModule: function (options) { return '<div class="km-popModule"><div class="km-pop-container"><p class="km-container-hd">' + options[options.popType].title + '</p><div class="km-container-img"></div><button class="km-container-link km-app">' + options[options.popType].btnWords + '<i class="km-star"></i></button><button class="km-pop-close" data-poptype="' + options.popType + '">继续用网页看漫画</button></div></div>' }
        },
        styles: {
            hdBanner: function (options) {
                return '<style>\
                    .km-hd-banner {position: fixed;top: 0;z-index: 9999;box-sizing: border-box;display: -webkit-flex;display: flex;-webkit-flex-flow: row nowrap;flex-flow: row nowrap;-webkit-align-items: center;-webkit-align-items: center;align-items: center;width: 100%;height: 44px;padding: 0 21px;background-color: rgba(0, 0, 0, .8);}\
                    .km-hd-banner > .km-banner-logo {width: 24px; height: 24px;background: transparent;}\
                    .km-hd-banner > .km-banner-desc {-webkit-flex: 1;flex: 1;padding: 0 10px;font: 12px / 1.4 "Microsoft Yahei", SimSun, Tahoma, arial, SimHei, sans-serif;color: #fff;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;}\
                    .km-hd-banner > .km-banner-operate {display: -webkit-flex;display: flex;flex-flow: row nowrap;-webkit-align-items: center;align-items: center;}\
                    .km-hd-banner > .km-banner-operate > .km-operate-open {display: block;width: 48px;height: 24px;border-radius: 5px;background-color: rgb(242, 82, 103);text-align: center;line-height: 24px;color: #fff;}\
                    .km-hd-banner > .km-banner-operate > .km-operate-close {width: 7px;height: 7px;margin-left: 8px;background-image: url("'+ options.close + '");background-size: contain;}\
                    .hdBannerAnim{-webkit-transform:translate3d(0,0,0);transform: translate3d(0,0,0);-webkit-transition:all .5s cubic-bezier(.19, 1, .22, 1);transition: all .5s cubic-bezier(.19, 1, .22, 1);z-index:9999;}\
                    .hdBannerAnimHidden{-webkit-transform:translate3d(0,-101%,0);transform: translate3d(0,-101%,0);-webkit-transition:all .3s cubic-bezier(.55, .055, .675, .19);transition: all .3s cubic-bezier(.55, .055, .675, .19); z-index:9999;}\
                    <style>'
            },
            rdHdBanner: function (options) {
                return '<style>\
                    .km-rdhd-banner {position: fixed;top: 0;z-index: 9999;box-sizing: border-box;display: -webkit-flex;display: flex;-webkit-flex-flow: row nowrap;flex-flow: row nowrap;-webkit-align-items: center;align-items: center;width: 100%;height: 44px;padding: 0 21px;background-color: rgba(0, 0, 0, .8);}\
                    .km-rdhd-banner > .km-banner-logo {width: 24px; height: 24px;background: transparent;}\
					.km-rdhd-banner > .km-banner-desc {-webkit-flex: 1;flex: 1;padding: 0 10px;font: 12px / 1.4 "Microsoft Yahei", SimSun, Tahoma, arial, SimHei, sans-serif;color: #fff;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;}\
                    .km-rdhd-banner > .km-banner-operate {display: -webkit-flex;display: flex;-webkit-flex-flow: row nowrap;flex-flow: row nowrap;-webkit-align-items: center;align-items: center;}\
                    .km-rdhd-banner > .km-banner-operate > .km-operate-open {display: block;width: 48px;height: 24px;border-radius: 5px;background-color: rgb(242, 82, 103);text-align: center;line-height: 24px;color: #fff;}\
                    .km-rdhd-banner > .km-banner-operate > .km-operate-close {width: 7px;height: 7px;margin-left: 8px;background-image: url("'+ options.close + '");background-size: contain;}\
                    <style>'
            },
            ftBanner: function (options) {
                return '<style>\
                    .km-ft-banner {position: fixed;bottom: 0;z-index: 9999;box-sizing: border-box;display: -webkit-flex;display: flex;-webkit-flex-flow: row nowrap;flex-flow: row nowrap;-webkit-align-items: flex-end;align-items: flex-end;width: 100%;height: 52px;padding: 0 21px 0 10px;background-color: rgba(0, 0, 0, .8);}\
                    .km-ft-banner > .km-banner-logo {width: 70px;height: 110px;background:transparent}\
                    .km-ft-banner > .km-banner-container {display: -webkit-flex;display: flex;-webkit-flex: 1;flex: 1;-webkit-align-items: center;align-items: center;height: 100%;}\
                    .km-ft-banner > .km-banner-container > .km-banner-desc {-webkit-flex: 1;flex: 1;padding: 0 5px;font: 12px / 1.4 "Microsoft Yahei", SimSun, Tahoma, arial, SimHei, sans-serif;color: #fff;}\
                    .km-ft-banner > .km-banner-container > .km-banner-operate {display: -webkit-flex;display: flex;-webkit-flex-flow: row nowrap;flex-flow: row nowrap;-webkit-align-items: center;align-items: center;}\
                    .km-ft-banner > .km-banner-container > .km-banner-operate > .km-operate-open {display: block;width: 48px;height: 24px;border-radius: 5px;background-color: rgb(242, 82, 103);text-align: center;line-height: 24px;color: #fff;}\
                    .km-ft-banner > .km-banner-container > .km-banner-operate > .km-operate-close {width: 7px;height: 7px;margin-left: 8px;background-image: url("'+ options.close + '");background-size: contain;}\
                    .ftBannerAnim{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0);-webkit-transition:all .5s cubic-bezier(.19,1,.22,1);transition:all .5s cubic-bezier(.19,1,.22,1);z-index:9999;}\
                    .ftBannerAnimHidden{-webkit-transform:translate3d(0,110px,0);transform:translate3d(0,110px,0);-webkit-transition:all .3s cubic-bezier(.55,.055,.675,.19);transition:all .3s cubic-bezier(.55,.055,.675,.19);z-index:9999;}\
                    <style>'
            },
            offlineCacheBtn: function (options) {
                return '<style>' +
                    '.km-offlinecache {position: absolute;bottom: 0;right: -90px;width: 80px;margin: 0 5px;height: 32px;border: 1px solid rgb(119, 50, 2);border-radius: 3px;font: 13px / 1.4 "Microsoft Yahei", SimSun, Tahoma, arial, SimHei, sans-serif;text-align: center;color: #fff;background-color: rgb(242, 82, 103);}' +
                    '.km-offlinecache > .km-star {position: absolute;top: -9px;left: -6px;width: 20px;height: 19px;background-image: url("/static/bannerImg/icon_star.png");background-size: contain;}' +
                    '</style>'
            },
            commentsBtn: function (options) {
                return '<style>\
                    .km-commont-btn{width: 100%;margin: 20px 0;}\
                    .km-commont-btn > .km-btn{position: relative;display: block;margin: 0 auto;width: 80%;min-width: 270px;padding: 9px 0;border: 1px solid rgb(175, 8, 19);border-radius: 3px;text-align: center;font: 14px / 1.4 "Microsoft Yahei", SimSun, Tahoma, arial, SimHei, sans-serif;background-color: rgb(242, 82, 103);color: #fff;-webkit-box-reflect: below 0 -webkit-linear-gradient(transparent,transparent 75%,rgba(242, 82, 103, .4));}\
                    .km-commont-btn > .km-btn::after {position: absolute;top: -10px;left: -7px;width: 20px;height: 19px;content: "";text-align: center;background-image: url("/static/bannerImg/icon_star.png");background-size: contain;}\
                    </style>'
            },
            chapterEndTip: function (options) {
                return '<style>\
				.km-chapter-end-container{margin-bottom: 40px;}\
                .km-chapter-end-container > .km-btn-open {position: absolute;bottom: 0;left: 50%;-webkit-transform: translateX(-50%);transform: translateX(-50%);width: 194px;padding: 7px 0;border: 1px solid rgb(175, 8, 19);border-radius: 3px;text-align: center;font: 18px / 1.4 "Microsoft Yahei", SimSun, Tahoma, arial, SimHei, sans-serif;background-color: rgb(242, 82, 103);color: #fff;-webkit-box-reflect: below 0 -webkit-linear-gradient(transparent,transparent 75%,rgba(242, 82, 103, .4));}\
                .km-chapter-end-container > .km-btn-open::after {position: absolute;top: -9px;left: -5px;width: 20px;height: 19px;content: "";text-align: center;background-image: url("/static/bannerImg/icon_star.png");background-size: contain;}\
                </style>'
            },
            commicEndTip: function (options) {
				var height = document.body.clientWidth / 2 * 451 / 610 + 'px';
                return '<style>\
                .km-commic-end{position: fixed;top: 0;right: 0;bottom: 0;left: 0;width: 100%;background-color: #fff;}\
                .km-commic-end > .km-container {box-sizing: border-box;padding: 10px;display: -webkit-flex;display: flex;-webkit-flex-direction: column;flex-direction: column;-webkit-justify-content: space-between;justify-content: space-between;-webkit-align-items: center;align-items: center;height: 100%;}\
                .km-commic-end > .km-container > img.logo {display: block;width'+document.body.clientWidth / 2 * 375 / 250 +'px;max-width: 305px;height: '+height+';margin: 20px 0 0 0;width: auto;background: none;}\
                .km-commic-end > .km-container > .km-buttons{-webkit-flex: 1;flex: 1;}\
                .km-commic-end > .km-container > .km-buttons > button {display: block;width: 310px;height: 40px;margin-bottom: 21px;border: 1px solid rgb(182, 182, 182);border-radius: 3px;font: 300 15px / 1.4 "Microsoft Yahei", SimSun, Tahoma, arial, SimHei, sans-serif;}\
                .km-commic-end > .km-container > .km-buttons > .km-btn-open {position: relative;background-color: rgb(242, 82, 103);color: #fff;text-align: center;}\
                .km-commic-end > .km-container > .km-buttons > .km-btn-open::after {position: absolute;top: -9px;left: -5px;width: 20px;height: 19px;content: "";text-align: center;background-image: url("/static/bannerImg/icon_star.png");background-size: contain;}\
                .km-commic-end > .km-container > .km-buttons > .km-btn-close {font: 300 14px / 1.4 "Microsoft Yahei", SimSun, Tahoma, arial, SimHei, sans-serif;background-color: #fff;color: rgb(182, 182, 182);}\
                .km-commic-end > .km-container > .km-bottom-logo {width: 60px;}\
                </style>'
            },
            popModule: function (options) {
                return '<style id="km-popModule-style">\
                .km-popModule {position: fixed;top: 0;right: 0;bottom: 0;left: 0;background-color: rgba(0, 0, 0, .8);z-index: 9999;}\
                .km-popModule > .km-pop-container {display: -webkit-flex;display: flex;-webkit-flex-direction: column;flex-direction: column;-webkit-justify-content: flex-start;justify-content: flex-start;-webkit-align-items: center;align-items: center;position: absolute;top: 50%;left: 50%;width: 257px;height: 281px;-webkit-transform: translate(-50%, -50%);transform: translate(-50%, -50%);border-radius: 5px;box-sizing: border-box;padding: 25px 0;background-color: #fff;}\
                .km-popModule > .km-pop-container > .km-container-hd {font: 12px / 1.4 "Microsoft Yahei", SimSun, Tahoma, arial, SimHei, sans-serif;color: rgb(153, 153, 153);}\
                .km-popModule > .km-pop-container > .km-container-img {width: 110px;height: 107px;margin: 18px 0;background-image: url("'+ options[options.popType].img + '");background-size: contain;}\
                .km-popModule > .km-pop-container > .km-container-link {position:relative; width: 209px;height: 33px;margin-bottom: 5px;border: 1px solid rgb(175, 8, 19);border-radius: 3px;background-color: rgb(242, 82, 103);text-align: center;color: #fff;}\
                .km-popModule > .km-pop-container > .km-container-link > .km-star {position: absolute;top: -10px;left: -7px;width: 20px;height: 19px;content: "";text-align: center;background-image: url("/static/bannerImg/icon_star.png");background-size: contain;}\
                .km-popModule > .km-pop-container > .km-pop-close {width: 209px;height: 33px;border: 1px solid rgb(204, 204, 204);background-color: #fff;color: rgb(153, 153, 153);}\
                </style>'
            }
        },
        popOptions: {
            cache: { title: '无网状态毋担忧，离线缓存解烦忧', img: '/static/bannerImg/cache_img.png', btnWords: '打开APP离线看漫画' },
            share: { title: '这么好看的漫画，快去分享给小伙伴吧！', img: '/static/bannerImg/share_img.png', btnWords: '打开APP分享漫画' },
            read1st: { title: '从哪里停下，就从哪里出发！', img: '/static/bannerImg/read_img.png', btnWords: '打开APP阅读更方便' },
            read: { title: '从哪里停下，就从哪里出发！', img: '/static/bannerImg/read_img.png', btnWords: '打开APP阅读更方便' }
        },
        genOptions: {
            kanman: {
                ckey: 'CK1382108356199', // 看漫画微下载: CK1358783372301
                ios: 'id1243372811', // 看漫画:id1303470988
                appname: '看漫画', // 斗罗大陆IOS: id1303470988
                pkgname: 'com.wbxm.icartoon',
                scheme_Adr: 'tkanmanapp'
            },
            iyouman: {
                ckey: 'CK1383325844989', // 爱优漫微下载: CK1358783561013
                ios: 'id1243372811', // 斗罗大陆IOS:
                appname: '爱优漫',
                pkgname: 'com.comic.iyouman',
                scheme_Adr: 'tiyoumanapp'
            },
            shenmanhua: {
                ckey: 'CK1383322645544', // 神漫画微下载: CK1358783561013
                ios: 'id1243372811', // 斗罗大陆IOS:
                appname: '神漫画',
                pkgname: 'com.comic.shenmanhua',
                scheme_Adr: 'tshenmanhuaapp'
            },
            douluodalu3: {
                ckey: 'CK1358783561013', // 斗罗大陆微下载: CK1358783561013
                ios: 'id1243372811', // 斗罗大陆IOS:
                appname: '漫画台',
                pkgname: 'com.comic.manhuatai',
                scheme_Adr: 'tmanhuataiapp'
            },
            isaman: {
                ckey: 'CK1383328534446', // 爱飒漫画
                ios: 'id1243372811', //
                appname: '爱飒漫画',
                pkgname: 'com.comic.isaman',
                scheme_Adr: 'tmanhuataiapp'
            },
            manhuatai: {
                ckey: 'CK138297596322', // 漫画台微下载: CK1358783561013
                ios: 'id1243372811', // 漫画台IOS: id1243372811
                appname: '漫画台',
                pkgname: 'com.comic.manhuatai',
                scheme_Adr: 'tmanhuataiapp'
            },
            zymk: {
                ckey: 'CK1353950509167', // 知音漫客微下载: CK1353950509167
                ios: 'id1012491820', // 知音漫客IOS: id1012491820
                appname: '知音漫客',
                pkgname: 'cn.zymk.comic',
                scheme_Adr: 'zymkapp'
            }
        },
        isReadPage: false
    };

    function Banner(ele, options) {
        this.head = $('head');
        this.container = $(ele);
        this.options = options = $.extend(true, {}, defaults, options || {});
        this.init();
    };

    Banner.prototype = {
        init: function () {
            this.loadjs('http://static.321mh.com/js/clipboard.min.js', { async: true });
            this.options['appLink'] = this._generateConfig(this.options.genOptions);
            this._render(this.options);
            this.domAnim(44);
            this._HTMLstyle();
			this._commicEndTipResize();
        },
        /**
         * loadjs 同步或异步方式加载js文件 opt:
         * @param  {[type]} src 加载js地址
         * @param  {[type]} opt  {charset,id,data,async:true} 默认同步输出
         */
		loadjs: function (src, opt) {
            var doc = document;
			opt = opt || {};
			if (opt.async) {
				var obj = doc.createElement('script');
				obj.src = src;
				obj.async = true;
				obj.type = 'text/javascript';
				if (opt.charset) {
					obj.charset = opt.charset;
				}
				if (opt.data) {
					obj.data = opt.data;
				}
				if (opt.id) {
					obj.id = opt.id;
				}
				(doc.head || doc.getElementsByTagName('head')[0] || doc.docElement).appendChild(obj);
			} else {
				doc.write('<script src="' + src + '"' + (opt.charset ? ' charset="' + opt.charset + '"' : '') + (opt.data ? ' data="' + opt.data + '"' : '') + (opt.id ? ' id="' + opt.id + '"' : '') + '></script>');
			}
		},

        /**
         * 根据传入的配置选项，插入dom结构以及对应的样式表。
         */
        _render: function (options) {
            var _this = this;
            var doms = this._assembleDom(options);
            var styleSheet = this._assembleStyles(options);
            var eventsProxy = function (_this) {
                doms.forEach(function (item) { _this.container.append(item); });
                styleSheet.forEach(function (item) { _this.head.append(item); });
                _this._regEvents();
            }
            eventsProxy(_this);
        },

        /**
         * 页面dom注册事件。
         */
        _regEvents: function () {
            var _this = this;
            var Events = this._events();
            for (var key in Events) {
                if (Events.hasOwnProperty(key)) {
                    Events[key].call(null, _this);
                }
            }
        },

        /**
         * 根据传入的配置，组装完整的dom结构，并返回。
         */
        _assembleDom: function (options, popType) {
            var doms = this._bannerCookieTst(options.appendDoms);
            var domArray = [], domObject = defaults.doms;
            doms && doms.forEach(function (item) {
                domArray.push(domObject[item]($.extend({}, options.options[item], options.appLink || {})));
            });
            return domArray
        },

        /**
         * 根据传入的配置，组装完整的dom结构样式表，并返回。
         */
        _assembleStyles: function (options, popType) {
			var doms = this._bannerCookieTst(options.appendDoms);
            var styleArray = [], stylesObject = defaults.styles;
            doms && doms.forEach(function (item) {
                styleArray.push(stylesObject[item](options.options[item], popType));
            });
            return styleArray
        },

        /**
         * 各dom结构的事件方法
         */
        _events: function () {
            var events = {
                linkToApp: function (_this) {
                    $('.km-app').off().on('click', function (e) {
                        _this._schemeHandler(e);
                    });
                },
                popShow: function (_this) {
                    $('.km-pop .bds_count,.km-pop .bdsharebuttonbox').off();
                    $('.km-pop').off().on('click', function () {
                        if ($(this).data('poptype') === 'read') {
                            if (_this._cookie('readPopTimer')) {
                                events.goRead(_this, true);
                                return
                            };
                        }
                        var type = { popType: $(this).data('poptype') };
                        var options = { appendDoms: ['popModule'], options: { popModule: $.extend({}, _this.options.popOptions, type) } }
                        _this._renderPopImmediate(options, type.popType);
                        events.linkToApp(_this);
                    });
                },
                closeBanner: function (_this) {
                    $('.km-close').off().on('click', function () {
                        $(this).parents('.km-banner').remove();
                        _this._cookie($(this).data('name') + 'Timer', true, { expires: 0.2 });
						if ($(this).data('name') === 'hdBanner' || $(this).data('name') === 'rdHdBanner') {
							$('body').css({ 'margin-top': '0' });
						}
                        if ($(this).data('poptype') === 'commicEndPage') {
                            event.stopPropagation();
                            event.preventDefault();
                            // window.location.href = location.origin + '/' + location.pathname.match(/\d+/g)[0];
                            $('.km-commic-end').remove();
                            return;
                        }
                    });
                },
                goRead: function (_this, isReadPage) {
                    if (isReadPage) {
                        var cookieStr = _this._cookie('urecord');
                        var pathname = location.pathname;
                        var origin = location.origin;
                        var commicId = pathname.match(/\d+/g)[0];
                        var recordArray = eval(_this._cookie('urecord'));
                        var page = $('.chapter-list li:eq(0) a').attr('href');
                        if (recordArray) {
                            var flag = false;
                            recordArray.forEach(function (item) {
                                if (item.indexOf(commicId) != -1) {
                                    flag = true;
                                    window.location.href = origin + '/' + commicId + '/' + item[2] + '.html';
                                }
                            })

                            if (!flag && page){
                                window.location.href = origin + '/' + commicId + '/' + page;
                            }else{
                                alert('该漫画暂时没有章节！');
                            }

                        } else {
                            if(page){
                                window.location.href = origin + '/' + commicId + '/' + page;
                            }else{
                                alert('该漫画暂时没有章节！');
                            }
                        }
                    }
                }
            };
            return events;
        },

        _renderPopImmediate: function (options, popType) {
            options['appLink'] = this.options.appLink;
            var _this = this;
            var popDom = this._assembleDom(options, popType);
            var popStyle = this._assembleStyles(options, popType);
            $('body').append(popDom[0]);
            $('head').append(popStyle[0]);
            $('.km-pop-close').off().on('click', function (event) {
                event.stopPropagation();
                event.preventDefault();
                if ($(this).data('poptype') === 'read') {
                    _this._cookie('readPopTimer', true, { expires: 0.5 });
                    _this._events()['goRead'](_this, true);
                }
                $('.km-popModule').remove();
                $('#km-popModule-style').remove();
            });
        },

        /**
         * 生成跳转链接
         */
        _generateConfig: function (options) {
            var generator = {};
            // var deviceGPU = this._deviceGPU();
            generator.getConfig = function (options) {
                var host = 'iyouman' || generator.getHost(); // 获得host
                var data = options[host];
                data['host'] = host; // 设置host的值

                // 将ios导量到看漫画
                var UA = navigator.userAgent.toLowerCase();
                if (/(iPhone|iPod|iPad);?/i.test(UA)) {
                    data = {
                        ckey: data.ckey, // 看漫画微下载: CK1358783372301
                        ios: 'id1243372811', // 斗罗大陆IOS:
						appname: '爱优漫',
						pkgname: 'com.comic.iyouman',
						scheme_Adr: 'tiyoumanapp'
                      }
                }
                
                return $.extend(data, {
                    url: 'http://a.app.qq.com/o/simple.jsp?pkgname=' + data.pkgname + '&ckey=' + data.ckey,
                    // iosurl: 'https://itunes.apple.com/app/apple-store/' + data.ios,
                    scheme_Adr: generator.getScheme(data)
                });
            };
            generator.getHost = function () {
                var host = location.host.replace(/([a-z]{1,6}\.)?(.+)(\.[a-z]{2,4})(\:\d+)?$/, '$2');
                if (/localhost|127\.0\.0\.1/.test(host)) {
                    host = 'iyouman';
                }
                return host;
            };
            generator.getScheme = function (data) {

                if (data.host === 'zymk') {
                    var match = location.pathname.match(/^\/\d+/g);
                    var comic_id = match ? location.pathname.match(/^\/\d+/g)[0].substr(1) : ''; // 获取到漫画的目录id
                    // return data.scheme_Adr + '://goto?page=' + (comic_id ? 'dir' : 'index') + '&comic_id=' + comic_id;
                    return data.scheme_Adr + '://goto?page=' + (comic_id ? 'dir' : 'index') + '&comic_id=' + comic_id
                } else {
                    var gobalInfo = window.gobal_info || {}; // 从文档中获取gobal_info
                    var comic_id = gobalInfo.comic_id || '';
                    return data.scheme_Adr + '://' + location.hostname.replace('m.', 'www.') + '/open?id=' + (comic_id ? comic_id : '');
                    // return data.scheme_Adr + '://goto?page=' + (comic_id ? 'dir' : 'index') + '&comic_id=' + comic_id
                }
            }
            generator.config = generator.getConfig(options);
            return generator.config;
        },
        _deviceGPU: function(){
            var canvas = document.createElement('canvas'),
            gl = canvas.getContext('experimental-webgl'),
            debugInfo = gl.getExtension('WEBGL_debug_renderer_info');
            return (gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL) || '');
        },
        /**
         * 跳转链接处理方法
         */
        _schemeHandler: function (e) {
            var _this = this;
            var config = _this.options['appLink'];
            var UA = navigator.userAgent.toLowerCase();
            var schemeAdr = config.scheme_Adr;

            // 添加剪切板拷贝功能
			e.currentTarget.setAttribute('data-schemes', schemeAdr)
			Clipboard && new Clipboard('.km-app', {
				text: function (trigger) {
					return trigger.getAttribute('data-schemes')
                }})
			// 添加剪切板拷贝功能结束
			// var deviceGPU = this._deviceGPU();
            if (/(iPhone|iPod|iPad);?/i.test(UA)) {

                //抛出schemes,打开app对应页面
                window.location.href = schemeAdr;

                //由于部分ios中打开app后，WAP页面会被挂起，定时器不会被执行。因此可以做下优化：
                //WAP页重新被聚焦后，如果超过1s，认为APP被打开了，重新聚焦时就不必再跳转到APP下载页

                var startTime = Date.now();
                var delayTime = /safari/.test(UA) ? 3000 : 1000;
                var timer = setInterval(function () {
                    var endTime = Date.now();
                    if (endTime - startTime > delayTime) {
                        clearInterval(timer);
                        window.location.href = config.iosurl || config.url;
                    }
                }, 25);
            } else {
                _this.creatIframe(schemeAdr);
                //跳转下载页

                setTimeout(function () {
                    window.location.href = config.url;
                }, 600);
            }
        },

        creatIframe: function (src) {
            // 如果用户已经安装了应用 抛出schemes,打开app对应页面
            var ifr = document.createElement('iframe');
            ifr.style.display = 'none';
            ifr.src = src;
            document.body.appendChild(ifr);
        },

        domAnim: function (dis) {
            $(window).scroll(function () {
                var hdbanner = $('.km-hd-banner'),
                    ftbanner = $('.km-ft-banner');
                if ($(window).scrollTop() > dis) {
                    hdbanner.attr('class', 'km-hd-banner km-banner hdBannerAnimHidden');
                    ftbanner.attr('class', 'km-ft-banner km-banner ftBannerAnim');
                } else {
                    hdbanner.attr('class', 'km-hd-banner km-banner hdBannerAnim');
                    ftbanner.attr('class', 'km-ft-banner km-banner ftBannerAnimHidden');
                }
            })
        },

        /**
         * 简单cookie获取与设置
         * @param  {string} name  cookie设置或获取
         * @param  {string} value 设置cookie的值
         * @param  {string} opt 设置cookie的参数
         */
        _cookie: function (name, value, opt) {
            // 变量获取
            var op = opt || {},
                expires = '',
                d = new Date();
            if (typeof value != 'undefined') {
                if (value === null) {
                    op.expires = -1
                }; //删除
                if (op.expires && (typeof op.expires == "number" || op.expires.toUTCString)) {
                    if (typeof op.expires == "number") {
                        d.setTime(d.getTime() + (op.expires * 36e5))
                    } // 以小时为单位
                    else {
                        d = op.expires
                    }
                    expires = "; expires=" + d.toUTCString();
                }
                document.cookie = [name, "=", encodeURIComponent(value), expires, op.path ? ("; path=" + op.path) : "", op.domain ? ("; domain=" + op.domain) : "", op.secure ? "; secure" : ""].join('');
            } else {
                return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + name + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null
            }
        },

        _HTMLstyle: function () {
			var isBannerExist = $('.km-hd-banner').length || $('.km-rdhd-banner').length;
			if (isBannerExist) {
				$('body').css({ 'margin-top': '54px' });
			}
            //if (navigator.userAgent.toLowerCase().search(/(ipad);?/i) !== -1) {
            //    $('html, body').css({ 'width': '100%', 'height': '100%', 'position': 'fixed', 'overflow-y': 'auto' });
            //}
        },

        // 根据cookie检测顶部和底部的banner是否出现
        _bannerCookieTst: function (array) {
            var _this = this, doms = [];
            for (var index = 0; index < array.length; index++) {
                !_this._cookie(array[index] + 'Timer') && doms.push(array[index])
            }
            return doms
        },
		
		_commicEndTipResize: function () {
			var container = $('.km-commic-end .km-container');
			if (container.length) {
				container.parent()[0].ontouchstart = function (e) {
					var target = $(e.target);
					if (!target.hasClass('km-btn-open') && !target.hasClass('km-btn-close')){
						e.preventDefault();
						return false
					} else {
						container.parent()[0].ontouchmove = function (e) {
							e.preventDefault();
							false;
						}
						return true
					}
				}
				
				function adapt() {
					var scWidth = $(window).width();
					var scHeight = $(window).height();
					if (scWidth > scHeight) {
						container.css({'-webkit-transform': 'scale('+ scHeight / scWidth +')','transform': 'scale('+ scHeight / scWidth +')', 'transform-origin': '50% 0','-webkit-transform-origin': '50% 0', 'height': 'auto'});
					} else {
						container.css({'-webkit-transform': 'scale(1)','transform': 'scale(1)', 'transform-origin': '50% 0','-webkit-transform-origin': '50% 0', 'height': '100%'});
					}
				}
				
				adapt();
				$(window).resize(function () {
					adapt();
				});
			}
		}
    };

    $.fn.Banner = function (options) {
        return new Banner(this, options);
    };
}(jQuery, window, document));
